<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/3/3
 * Time: 上午10:18
 */
namespace Home\Controller;
use Common\Controller\HomeBaseController;

class CartController extends HomeBaseController
{
    /**
     * 添加到购物车
     */
    public function add_cart()
    {
        if(IS_POST){
            //接收前段传来的数据
            $goods_number = I('post.goods_number',0);
            $goods_id = I('post.goods_id',0);
            $fd_price = I('post.fd_price/f',0.00);
            if(empty($goods_number) || empty($goods_id) || empty($fd_price)){
                $this->ajaxReturn(['message'=>'接收信息失败！','error'=>7]);
            }
            $one_goods_info = D('goods')->get_one_info(['id'=>$goods_id],'goods_name,litimg');
            if(!isset($one_goods_info['litimg'])){
                $this->ajaxReturn(['message'=>'获取商品信息失败！','error'=>7]);
            }
            //$this->ajaxReturn(['message'=>$one_goods_info['goods_name'],'error'=>7]);
            $one_goods_info['goods_id'] = $goods_id;
            $one_goods_info['fd_price'] = $fd_price;
            $one_goods_info['num'] = $goods_number;
            $one_goods_info['subtotal'] = $goods_number * $fd_price;
            //判断用户是否登录
            if(empty(session(C('FRONT_SESSION')))){
                //cookie('goods',[],array('expire'=>-36000,'prefix'=>'fd_'));
                //没登录用户用cookie存储购物车信息
                if(isset($_COOKIE['fd_goods'])){
                    //如果是第二次添加商品就反序列化添加商品后再系列化存入cookie
                    $goods = $_COOKIE['fd_goods'];
                    $goods = unserialize($goods);
                    //循环判断该商品在cookie中是否存在,存在就直接加数量和小计,然后返回信息结束
                    foreach($goods as $k=>$v){
                        if($v['goods_id'] == $one_goods_info['goods_id']){
                            $goods[$k]['num'] += $one_goods_info['num'];
                            $goods[$k]['subtotal'] += $one_goods_info['subtotal'];
                            $goods = serialize($goods);
                            cookie('goods',$goods,array('expire'=>36000,'prefix'=>'fd_'));
                            $this->ajaxReturn(['error'=>0,'one_step_buy'=>0]);
                        }
                    }
                    //该商品不存在继续添加一个商品
                    $goods[] = $one_goods_info;
                    $goods = serialize($goods);
                    cookie('goods',$goods,array('expire'=>36000,'prefix'=>'fd_'));
                }else{
                    //如果是第一次添加商品，就直接序列化存入cookie
                    $goods = array();
                    $goods[] = $one_goods_info;
                    $goods = serialize($goods);
                    cookie('goods',$goods,array('expire'=>36000,'prefix'=>'fd_'));
                }
                $this->ajaxReturn(['error'=>0,'one_step_buy'=>0]);
            }
            //如果是登录过的用户就存入数据库
            $cart = M('cart');
            $cart_info = $cart->where(['mid'=>session(C('FRONT_SESSION'))['id'],'goods_id'=>$goods_id])->find('id','num','subtotal');
            //购物车中已经存在就直接加数量和小计
            if(isset($cart_info['id'])){
                $data['num'] = $one_goods_info['num'] + $cart_info['num'];
                $data['subtotal'] = $one_goods_info['subtotal'] + $cart_info['subtotal'];
                if($cart->where(['id'=>$cart_info['id']])->save($data)){
                    $this->ajaxReturn(['error'=>0,'one_step_buy'=>0]);
                }
                $this->ajaxReturn(['message'=>'添加到购物车失败！','error'=>7]);
            }
            //购物车中不存在就添加一个商品
            $one_goods_info['mid'] = session(C('FRONT_SESSION'))['id'];
            if($cart->add($one_goods_info)){
                $this->ajaxReturn(['error'=>0,'one_step_buy'=>0]);
            }
            $this->ajaxReturn(['message'=>'添加到购物车失败！','error'=>7]);
        }else{
            $this->ajaxReturn(['message'=>$_SESSION,'error'=>7]);
        }
    }

    /**
     * 加载购物车
     */
    public function cart()
    {
        if(empty(session(C('FRONT_SESSION')))){
            if(isset($_COOKIE['fd_goods'])){
                $goods = $_COOKIE['fd_goods'];
                $cart_goods = unserialize($goods);
            }else{
                $cart_goods = array();
            }
        }else{
            $cart_goods = M('cart')->where(['mid'=>session(C('FRONT_SESSION'))['id']])->select();
        }
        $count = 0;
        $all_price = 0.00;
        foreach($cart_goods as $v){
            $count +=  $v['num'];
            $all_price += $v['subtotal'];
        }
        $this->assign('count',$count);
        $this->assign('all_price',$all_price);
        $this->assign('cart_goods',$cart_goods);
        $this->assign('title','购物流程');
        $this->display();
    }

    /**
     * 修改购物车数据
     */
    public function update_cart()
    {
        if(IS_POST){
            $goods_id = I('post.rec_id',0);
            if(empty($goods_id)){
                $this->ajaxReturn(['error'=>1,'message'=>'非法操作！']);
            }
            $goods_num = I('post.goods_number',0);
            if(empty($goods_num)){
                $this->ajaxReturn(['error'=>1,'message'=>'非法操作！']);
            }
            if(empty(session(C('FRONT_SESSION')))){
                //没登录则对cookie进行操作
                if(isset($_COOKIE['fd_goods'])){
                    //存在商品才开始遍历匹配更新
                    $goods = $_COOKIE['fd_goods'];
                    $cart_goods = unserialize($goods);
                    //统计总商品数量和总价钱
                    $all_num = 0;
                    $all_price = 0.00;
                    foreach($cart_goods as $k=>$v){
                        if($v['goods_id'] == $goods_id){
                            //如果匹配到
                            $goods_subtotal = $goods_num * $v['fd_price'];  //得到该商品小计
                            $all_num += $goods_num;
                            $all_price += $goods_subtotal;
                            $cart_goods[$k]['num'] = $goods_num;
                            $cart_goods[$k]['subtotal'] = $goods_subtotal;
                            $goods = serialize($cart_goods);
                            cookie('goods',$goods,array('expire'=>36000,'prefix'=>'fd_'));
                        }else{
                            $all_num += $v['num'];
                            $all_price += $v['subtotal'];
                        }
                    }
                    $this->ajaxReturn(['error'=>0,'rec_id'=>$goods_id,'goods_number'=>$goods_num,'goods_subtotal'=>'￥'.$goods_subtotal,'total_price'=>'￥'.$all_price,'total_goods_count'=>$all_num]);
                }
            }else{
                //登录了就对数据库进行更新
                $cart = M('cart');
                $goods_info = $cart->field('id,fd_price')->where(['mid'=>session(C('FRONT_SESSION'))['id'],'goods_id'=>$goods_id])->find();
                if(empty($goods_info)){
                    $this->ajaxReturn(['error'=>1,'message'=>'购物车中不存在该商品！']);
                }
                $goods_subtotal = $goods_num * $goods_info['fd_price'];
                if(!$cart->where(['id'=>$goods_info['id']])->save(['num'=>$goods_num,'subtotal'=>$goods_subtotal])){
                    $this->ajaxReturn(['error'=>1,'message'=>'修改数量失败！']);
                }
                $all_cart_goods = $cart->where(['mid'=>session(C('FRONT_SESSION'))['id']])->select();
                //统计总商品数量和总价钱
                $all_count = 0;
                $all_price = 0.00;
                foreach($all_cart_goods as $v){
                    $all_count +=  $v['num'];
                    $all_price += $v['subtotal'];
                }
                $this->ajaxReturn(['error'=>0,'rec_id'=>$goods_id,'goods_number'=>$goods_num,'goods_subtotal'=>'￥'.$goods_subtotal,'total_price'=>'￥'.$all_price,'total_goods_count'=>$all_count]);
            }
            $this->ajaxReturn(['error'=>1,'message'=>'出现了未知的错误，修改失败！']);
        }else{
            $this->error('非法操作！');
        }

    }
    
    /**
     * 移除一个商品
     */
    public function out_cart()
    {
        if(IS_POST){
            $goods_id = I('post.id',0);
            if(empty($goods_id)){
                $this->ajaxReturn(['error'=>1,'err_msg'=>'移除该商品失败！']);
            }
            if(empty(session(C('FRONT_SESSION')))){
                if(isset($_COOKIE['fd_goods'])){
                    //存在商品才开始遍历匹配删除
                    $goods = $_COOKIE['fd_goods'];
                    $cart_goods = unserialize($goods);
                    foreach($cart_goods as $k=>$v){
                        if($v['goods_id'] == $goods_id){
                            unset($cart_goods[$k]);
                            $goods = serialize($cart_goods);
                            cookie('goods',$goods,array('expire'=>36000,'prefix'=>'fd_'));
                            $this->ajaxReturn(['error'=>0,'msg'=>'移除该商品成功！']);
                        }
                    }
                }
            }else{
                $cart = M('cart');
                $cart_id = $cart->where(['mid'=>session(C('FRONT_SESSION'))['id'],'goods_id'=>$goods_id])->getField('id');
                if(empty($cart_id)){
                    $this->ajaxReturn(['error'=>1,'err_msg'=>'购物车中不存在该商品！']);
                }
                if($cart->delete($cart_id)){
                    $this->ajaxReturn(['error'=>0,'msg'=>'移除该商品成功！']);
                }
            }
            $this->ajaxReturn(['error'=>1,'err_msg'=>'移除该商品失败！']);
        }else{
            $goods_id = I('get.id',0);
            if(empty($goods_id)){
                $this->error('非法操作！');
            }
            if(empty(session(C('FRONT_SESSION')))){
                if(isset($_COOKIE['fd_goods'])){
                    //存在商品才开始遍历匹配删除
                    $goods = $_COOKIE['fd_goods'];
                    $cart_goods = unserialize($goods);
                    foreach($cart_goods as $k=>$v){
                        if($v['goods_id'] == $goods_id){
                            unset($cart_goods[$k]);
                            $goods = serialize($cart_goods);
                            cookie('goods',$goods,array('expire'=>36000,'prefix'=>'fd_'));
                            $this->success('移除该商品成功！',U('cart'));
                            exit;
                        }
                    }
                }
            }else{
                $cart = M('cart');
                $cart_id = $cart->where(['mid'=>session(C('FRONT_SESSION'))['id'],'goods_id'=>$goods_id])->getField('id');
                if(empty($cart_id)){
                    $this->error('购物车中不存在该商品！');
                }
                if($cart->delete($cart_id)){
                    $this->success('移除该商品成功！',U('cart'));
                    exit;
                }
            }
            $this->error('移除该商品失败！');
        }
    }
    
    /**
     * 清空购物车
     */
    public function clear_cart()
    {
        if(IS_GET){
            if(empty(session(C('FRONT_SESSION')))){
                if(isset($_COOKIE['fd_goods'])){
                    //存在商品就清空
                    cookie('goods',1,array('expire'=>-36000,'prefix'=>'fd_'));
                    $this->success('清空购物车成功！',U('cart'));
                    exit;
                }
                $this->error('清空购物车失败！');
            }else{
                if(M('cart')->where(['mid'=>session(C('FRONT_SESSION'))['id']])->delete() === false){
                    $this->error('清空购物车失败！');
                }
                $this->success('清空购物车成功！',U('cart'));
            }
        }else{
            $this->error('非法操作！');
        }
    }
    
    /**
     * 结算
     */
    public function check_out()
    {

    }
}